
<!DOCTYPE html>
<html lang="en" class="loading">
<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta name="viewport" content="width=device-width, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>C语言快速入门(1) - 小林的博客</title>
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <meta name="google" content="notranslate" />
    <meta name="keywords" content="Fechin,"> 
    <meta name="description" content="1. 第一个C语言程序因为小林用的是VS2019 编译器，所以和大家会有差异。
首先打开编译器，创建项目。
然后选空项目，下一步。
然后创建项目
然后新建项
我们选择Cpp文件，切记，文件后缀名一定,"> 
    <meta name="author" content="John Doe"> 
    <link rel="alternative" href="atom.xml" title="小林的博客" type="application/atom+xml"> 
    <link rel="icon" href="/my_blog.gitee.io/img/favicon.png"> 
    
<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.css">

    
    
    <meta name="twitter:card" content="summary"/>
    <meta name="twitter:title" content="C语言快速入门(1) - 小林的博客"/>
    <meta name="twitter:description" content="1. 第一个C语言程序因为小林用的是VS2019 编译器，所以和大家会有差异。
首先打开编译器，创建项目。
然后选空项目，下一步。
然后创建项目
然后新建项
我们选择Cpp文件，切记，文件后缀名一定,"/>
    
    
    
    
    <meta property="og:site_name" content="小林的博客"/>
    <meta property="og:type" content="object"/>
    <meta property="og:title" content="C语言快速入门(1) - 小林的博客"/>
    <meta property="og:description" content="1. 第一个C语言程序因为小林用的是VS2019 编译器，所以和大家会有差异。
首先打开编译器，创建项目。
然后选空项目，下一步。
然后创建项目
然后新建项
我们选择Cpp文件，切记，文件后缀名一定,"/>
    
<link rel="stylesheet" href="/my_blog.gitee.io/css/diaspora.css">

    <script>window.searchDbPath = "/my_blog.gitee.io/search.xml";</script>
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Source+Code+Pro&display=swap" rel="stylesheet">
<meta name="generator" content="Hexo 6.3.0"></head>

<body class="loading">
    <span id="config-title" style="display:none">小林的博客</span>
    <div id="loader"></div>
    <div id="single">
    <div id="top" style="display: block;">
    <div class="bar" style="width: 0;"></div>
    <a class="iconfont icon-home image-icon" href="javascript:;" data-url="https://lin2c.gitee.io/my_blog.gitee.io"></a>
    <div title="播放/暂停" class="iconfont icon-play"></div>
    <h3 class="subtitle">C语言快速入门(1)</h3>
    <div class="social">
        <div>
            <div class="share">
                <a title="获取二维码" class="iconfont icon-scan" href="javascript:;"></a>
            </div>
            <div id="qr"></div>
        </div>
    </div>
    <div class="scrollbar"></div>
</div>

    <div class="section">
        <div class="article">
    <div class='main'>
        <h1 class="title">C语言快速入门(1)</h1>
        <div class="stuff">
            <span>二月 14, 2023</span>
            
  <ul class="post-tags-list" itemprop="keywords"><li class="post-tags-list-item"><a class="post-tags-list-link" href="/my_blog.gitee.io/tags/C%E8%AF%AD%E8%A8%80/" rel="tag">C语言</a></li><li class="post-tags-list-item"><a class="post-tags-list-link" href="/my_blog.gitee.io/tags/%E5%8F%98%E9%87%8F/" rel="tag">变量</a></li><li class="post-tags-list-item"><a class="post-tags-list-link" href="/my_blog.gitee.io/tags/%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B/" rel="tag">数据类型</a></li></ul>


        </div>
        <div class="content markdown">
            <h1 id="1-第一个C语言程序"><a href="#1-第一个C语言程序" class="headerlink" title="1. 第一个C语言程序"></a>1. 第一个C语言程序</h1><p>因为小林用的是VS2019 编译器，所以和大家会有差异。</p>
<p>首先打开编译器，创建项目。<br><img src="https://img-blog.csdnimg.cn/d18ce81be9cf42c0a8af6c8628671b16.png" alt="在这里插入图片描述"></p>
<p>然后选空项目，下一步。<br><img src="https://img-blog.csdnimg.cn/4b948a959e384aeebaffc10ab7ab1d70.png" alt="在这里插入图片描述"></p>
<p>然后创建项目<br><img src="https://img-blog.csdnimg.cn/3efb13c8c77e424ea21b78e96e8ddb41.png" alt="在这里插入图片描述"></p>
<p>然后新建项<br><img src="https://img-blog.csdnimg.cn/751d219c915f461f949f45df3dea19e7.png" alt="在这里插入图片描述"></p>
<p>我们选择Cpp文件，切记，文件后缀名一定要改成 .c ，然后添加<br><img src="https://img-blog.csdnimg.cn/6aa9db6522ff450c9f9edad8f6b7c8bb.png" alt="在这里插入图片描述"></p>
<p>然后我们就进到了以下界面，如果编译器是 2019的话，那么需要加上第一行来屏蔽输入警告，其他编译器则不用，所以不用担心。<br><img src="https://img-blog.csdnimg.cn/a0d8df1a88da43518edcd0658b745f5a.png" alt="在这里插入图片描述"></p>
<p>然后就可以开始敲代码了。我们先敲一个main函数，main函数是程序的执行入口，也就意味着任何一个C语言程序 都是从main函数开始执行的。<br><img src="https://img-blog.csdnimg.cn/162e72dfe9b34a2a9833737d51481713.png" alt="在这里插入图片描述"></p>
<p>然后我们写一个输出语句。<br><img src="https://img-blog.csdnimg.cn/7ddce2f3d7294c7eacaf3aecfaefca2d.png" alt="在这里插入图片描述"><br>再引上对应的头文件。因为printf函数 是C语言提供的，如果我们要使用，需要和它打个”招呼”，也就是引用头文件。使用printf需要引用&#x3D;&#x3D;stdio.h&#x3D;&#x3D;的头文件。</p>
<p><img src="https://img-blog.csdnimg.cn/63bf05f23aef4195a9c62927375d8929.png" alt="在这里插入图片描述"></p>
<p>然后我们运行一下程序，VS编译器环境下的运行快捷键是ctrl+f5。</p>
<p><img src="https://img-blog.csdnimg.cn/9a4cc4f6d8fb4d83995a4af93bc13b16.png" alt="在这里插入图片描述"><br>我们就发现我们的结果打印出来了，这就是第一个C语言程序。</p>
<hr>
<h1 id="2-数据类型"><a href="#2-数据类型" class="headerlink" title="2. 数据类型"></a>2. 数据类型</h1><p>C语言中常用的数据类型有以下几种</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">char</span> 字符型   <span class="number">1</span>字节</span><br><span class="line"><span class="type">short</span> 短整形  <span class="number">2</span>字节</span><br><span class="line"><span class="type">int</span>  整形     <span class="number">4</span>字节</span><br><span class="line"><span class="type">long</span> 长整型   不小于<span class="number">4</span>字节</span><br><span class="line"><span class="type">float</span> 单精度浮点型  <span class="number">4</span>字节</span><br><span class="line"><span class="type">double</span> 双精度浮点型 <span class="number">8</span>字节</span><br></pre></td></tr></table></figure>
<p>字符型 ： 1个字符，对应ASCII 码值<br>短整形 ：范围在 -32768（-2^15） ~  32767（2^15 - 1）之间的整数<br>整形 :  范围在  (-2)^31  ~  （2^31）-1  之间的整数<br>长整形 : C语言没有明确规定，只要求long的大小不能小于int。<br>单精度浮点型 : 简单理解就是小数。<br>双精度浮点型 ：精度更高的小数。</p>
<p>ASCII表<br><img src="https://img-blog.csdnimg.cn/bec30ea921e54d1d83105d70b9b6dfaf.png#pic_center" alt="在这里插入图片描述"><br>那我们写段代码来测试一下</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">char</span> ch = <span class="string">&#x27;a&#x27;</span>; <span class="comment">//字符型变量创建</span></span><br><span class="line">	<span class="type">short</span> sh = <span class="number">20</span>; <span class="comment">//短整型变量创建</span></span><br><span class="line">	<span class="type">int</span> i = <span class="number">30</span>; <span class="comment">//整形变量创建</span></span><br><span class="line">	<span class="type">long</span> lg = <span class="number">40</span>;  <span class="comment">//长整型变量创建</span></span><br><span class="line">	<span class="type">float</span> f= <span class="number">5.5f</span>; <span class="comment">//单精度浮点型变量创建</span></span><br><span class="line">	<span class="type">double</span> d = <span class="number">6.6</span>; <span class="comment">//双精度浮点型变量创建</span></span><br><span class="line"></span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;字符型:%c   所占字节: %d\n&quot;</span>,ch,<span class="keyword">sizeof</span>(ch));</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;短整形:%d  所占字节: %d\n&quot;</span>, sh, <span class="keyword">sizeof</span>(sh));</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;整形:%d    所占字节: %d\n&quot;</span>, sh, <span class="keyword">sizeof</span>(i));</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;长整形:%d  所占字节: %d\n&quot;</span>, lg, <span class="keyword">sizeof</span>(lg));</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;单精度浮点型:%f  所占字节: %d\n&quot;</span>, f ,<span class="keyword">sizeof</span>(f));</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;双精度浮点型:%lf  所占字节: %d\n&quot;</span>, d, <span class="keyword">sizeof</span>(d));</span><br><span class="line"></span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>sizeof 是一个关键字，它会返回一个数在内存所占的字节个数。<br><img src="https://img-blog.csdnimg.cn/b76c2291fe0248389ea071119e8aaf50.png" alt="在这里插入图片描述"></p>
<hr>
<h1 id="3-变量，常量"><a href="#3-变量，常量" class="headerlink" title="3. 变量，常量"></a>3. 变量，常量</h1><blockquote>
<p>生活中总有些值是不变的(圆周率，性别，身份证号码，血型)。<br>也总一些值是会变的(年龄，工资，体重) 等等<br>而会变的值在C语言中被称为变量，不变的值被称为常量。<br>所以变量通常用来存储会变的值，常量用来存储不变的值。</p>
</blockquote>
<h2 id="3-1-变量的定义"><a href="#3-1-变量的定义" class="headerlink" title="3.1 变量的定义"></a>3.1 变量的定义</h2><p>定义变量很简单，只需要 (数据类型名) + 变量名 ，即可定义一个变量。<br>假设我要定义一个字符型变量，那么就是  char(数据类型名) + ch(变量名);即可定义成功。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">char</span> ch; <span class="comment">//定义字符型变量</span></span><br><span class="line">	<span class="type">int</span> i; <span class="comment">//定义整形变量</span></span><br><span class="line">	<span class="type">long</span> l;<span class="comment">//定义长整型变量</span></span><br><span class="line">	<span class="type">float</span> f; <span class="comment">//定义浮点型变量</span></span><br><span class="line">	........</span><br><span class="line">	<span class="keyword">return</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>变量定义成功了，但是变量肯定会有值。如果我们不主动给变量赋值，那么局部变量的值就是随机值。所以我们要给变量赋值，赋值的方法也很简单。<br>变量名 &#x3D; 值;</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">char</span> ch; <span class="comment">//定义字符型变量</span></span><br><span class="line">	<span class="type">int</span> i; <span class="comment">//定义整形变量</span></span><br><span class="line">	<span class="type">long</span> l;<span class="comment">//定义长整型变量</span></span><br><span class="line">	<span class="type">float</span> f; <span class="comment">//定义浮点型变量</span></span><br><span class="line"></span><br><span class="line">	ch = <span class="string">&#x27;a&#x27;</span>; <span class="comment">//ch变量存储 &#x27;a&#x27; 这个字符</span></span><br><span class="line">	i = <span class="number">10</span>; <span class="comment">//i变量存储了 10这个整数</span></span><br><span class="line">	l = <span class="number">100</span>;<span class="comment">//l变量存储了100这个整数</span></span><br><span class="line">	f = <span class="number">5.5</span> ;<span class="comment">//f存储了5.5这个整数</span></span><br><span class="line">	........</span><br><span class="line">	<span class="keyword">return</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>变量存储了一个值之后，我们使用变量，就是使用这个值，比如说我们的变量i 存储了10，那么我们打印 i 就会打印10。<br><img src="https://img-blog.csdnimg.cn/da606208a1b0420abd3199ce4030f39a.png" alt="在这里插入图片描述"><br>变量也可以在定义的时候直接赋值。<br>数据类型名   变量名   &#x3D;  值</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">int</span> i = <span class="number">10</span>;</span><br><span class="line">	<span class="type">int</span> b = <span class="number">20</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="3-1-2-变量的命名规则"><a href="#3-1-2-变量的命名规则" class="headerlink" title="3.1.2 变量的命名规则"></a>3.1.2 变量的命名规则</h2><blockquote>
<p>变量的命名规则必须严格按照以下要求:</p>
<ol>
<li><p>只能由字母(大写或小写),数字和下划线(_)组成</p>
<ol start="2">
<li>不能超过63个字符</li>
<li>变量名区分大小写</li>
<li>不能以数字开头</li>
<li>变量名不能使用关键字</li>
</ol>
</li>
<li><p>同一个作用域不能出现相同的变量名</p>
</li>
</ol>
</blockquote>
<h2 id="3-2-变量的之间的运算"><a href="#3-2-变量的之间的运算" class="headerlink" title="3.2 变量的之间的运算"></a>3.2 变量的之间的运算</h2><blockquote>
<p>上面说到，变量是用来存储一个值的，那么这就意味着变量和变量之间可以存在运算。</p>
</blockquote>
<p>假设我有3个变量 ， 变量 a &#x3D; 10，变量  b &#x3D; 20， c用来存储a 和 b的和。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">int</span> a = <span class="number">10</span>;</span><br><span class="line">	<span class="type">int</span> b = <span class="number">20</span>;</span><br><span class="line">	<span class="type">int</span> c = a + b;</span><br><span class="line"></span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;c = %d\n&quot;</span>, c); </span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>c 存储的是 a+b的值，那我们打印c来看看。<br><img src="https://img-blog.csdnimg.cn/673ba076abc4424595b12dc72c486f8a.png" alt="在这里插入图片描述"></p>
<p>我们进行了 变量之间的加法运算，那我们分别来试试减法，乘法，除法。<br><img src="https://img-blog.csdnimg.cn/d2acd2d6d27345b0aba3d514f4b7a596.png" alt="在这里插入图片描述"><br>我们发现都可以进行运算，但是要注意一点的是，整数的除法运算只会取2个数的商，而不会取余数，比如 5 &#x2F; 2 的商是2，所以相除的结果是2。<br><img src="https://img-blog.csdnimg.cn/6b3abc7c53104ec2b8756828894f0bd1.png" alt="在这里插入图片描述"></p>
<h2 id="3-3-常量"><a href="#3-3-常量" class="headerlink" title="3.3 常量"></a>3.3 常量</h2><blockquote>
<p>变量是会变的值，那么常量自然就是不能变的值，变量和常量的区别就是，常量只能在定义的时候赋值，一旦常量定义完之后，那么常量的值无法被修改。</p>
</blockquote>
<h3 id="3-3-1-const常量"><a href="#3-3-1-const常量" class="headerlink" title="3.3.1 const常量"></a>3.3.1 const常量</h3><blockquote>
<p>const也是一个关键字，它可以让一个变量变成常量，但严格意义来说不是真正的常量。而是常变量，因为被 const修饰的常量可以通过特殊的方式修改它的值。当然，那是指针的内容了，这里我们不深入探讨。</p>
</blockquote>
<p>const修饰的常量定义：const  数据类型 变量名  &#x3D;  值</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">&#123;</span><br><span class="line"></span><br><span class="line">	<span class="type">const</span> <span class="type">int</span> i = <span class="number">10</span>;</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>const修饰过后，i的值就无法再被改变。我们尝试把i修改成20试试。<br><img src="https://img-blog.csdnimg.cn/4ec73d6bf7f54e8a8ded6f182fd44543.png" alt="在这里插入图片描述"><br>编译器就给我们报了个错误，因为常量无法修改。所以当你期望一个值无法被修改时，可以加const关键字对其修饰。</p>
<h3 id="3-3-2-define常量"><a href="#3-3-2-define常量" class="headerlink" title="3.3.2 define常量"></a>3.3.2 define常量</h3><blockquote>
<p>常量的另一种定义方法，就是用define关键字定义，这个是真正意义上的常量，一旦定义，谁也无法修改。</p>
</blockquote>
<p>定义方法： #define 变量名 值<br>这种常量建议定义成全局(就是在main函数外面创建)。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">define</span> i 10 <span class="comment">//常量定义</span></span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">&#123;</span><br><span class="line">	i = <span class="number">20</span>; <span class="comment">//尝试修改常量的值</span></span><br><span class="line">	</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>然后我们尝试修改i的值。<br><img src="https://img-blog.csdnimg.cn/119b9305e40943dab479b214aa84eac7.png" alt="在这里插入图片描述"><br>一样，报错。</p>
<h2 id="3-4-局部变量、全局变量"><a href="#3-4-局部变量、全局变量" class="headerlink" title="3.4 局部变量、全局变量"></a>3.4 局部变量、全局变量</h2><blockquote>
<p>局部变量，顾名思义，就是只能在一段范围内才能使用，一旦出来这个范围，那么就无法在使用这个变量。</p>
</blockquote>
<h3 id="3-4-1-作用域"><a href="#3-4-1-作用域" class="headerlink" title="3.4.1 作用域"></a>3.4.1 作用域</h3><p>在作用域范围内创建的局部变量，只能在当前作用域。什么是作用域呢？很简单，我们把 一段花括号{ } 之间的位置看成一个作用域。而在作用域里面创建的变量，出了作用域，就无法使用了。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">&#123;</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="type">int</span> i = <span class="number">10</span>;</span><br><span class="line">		i = <span class="number">20</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">//作用域外使用i</span></span><br><span class="line">	i = <span class="number">30</span>; </span><br><span class="line">	</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>我们会发现在作用域外使用 i ，会提示未定义。<br><img src="https://img-blog.csdnimg.cn/e2b5102ee00140b29aa292395acbd4a4.png" alt="在这里插入图片描述"></p>
<p>所以，i 就是所在作用域内的一个局部变量。一旦出了这个作用域， i 的生命周期也结束了。</p>
<h3 id="3-4-2-变量的生命周期"><a href="#3-4-2-变量的生命周期" class="headerlink" title="3.4.2 变量的生命周期"></a>3.4.2 变量的生命周期</h3><blockquote>
<p>每个变量都有自己的生命周期，一旦变量的生命周期结束了，就意味着这个变量被销毁了，不复存在了。而一个变量的生命周期，就是出了 它的作用域开始。</p>
</blockquote>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">int</span> a = <span class="number">10</span>;</span><br><span class="line"></span><br><span class="line">	&#123;<span class="comment">//作用域的开始</span></span><br><span class="line">		<span class="type">int</span> b = <span class="number">20</span>;</span><br><span class="line">		&#123; <span class="comment">//作用域开始</span></span><br><span class="line">			<span class="type">int</span> i = <span class="number">10</span>;</span><br><span class="line">			i = <span class="number">20</span>;</span><br><span class="line">		&#125;<span class="comment">//出了作用域后，i的生命周期结束了</span></span><br><span class="line">		</span><br><span class="line">	&#125;<span class="comment">//出了这个作用域后，b的生命周期结束了</span></span><br><span class="line">		</span><br><span class="line"></span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;<span class="comment">//出了这个作用域后，a的生命周期结束了</span></span><br></pre></td></tr></table></figure>
<p>如图，变量的生命周期，从变量的创建开始，到出作用域后结束。<br><img src="https://img-blog.csdnimg.cn/ff6b5274ca674781bb74381c4e026455.png" alt="在这里插入图片描述"></p>
<blockquote>
<p> 结论:<br> 1.局部变量的生命周期，从变量创建的所在作用域开始，到出此作用域结束。<br> 2.在当前作用域创建的变量，只能在当前作用域的范围内使用，一旦出了此作用域，那么该变量就无法使用，因为变量的生命周期结束，变量已经销毁。</p>
</blockquote>
<h2 id="3-5-全局变量"><a href="#3-5-全局变量" class="headerlink" title="3.5 全局变量"></a>3.5 全局变量</h2><p>局部变量的作用域为它创建时所在的作用域，那么全局变量的作用域就是整个程序。也就说全局变量在一个程序中的任何位置，都可以使用。 全局变量的生命周期就是整个程序的生命周期，程序不结束，全局变量不销毁。</p>
<p>全局变量定义：不在函数任意一个作用域里面定义的变量，就是全局变量。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span><span class="string">&lt;stdio.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> d = <span class="number">100</span>; <span class="comment">//全局变量定义</span></span><br><span class="line"></span><br><span class="line"><span class="type">int</span> <span class="title function_">main</span><span class="params">()</span></span><br><span class="line">&#123;</span><br><span class="line">	<span class="type">int</span> a = <span class="number">10</span>;</span><br><span class="line"></span><br><span class="line">	&#123;</span><br><span class="line">		<span class="type">int</span> b = <span class="number">20</span>;</span><br><span class="line">		&#123; </span><br><span class="line">			<span class="type">int</span> i = <span class="number">10</span>;</span><br><span class="line">			i = <span class="number">20</span>;</span><br><span class="line">		&#125;</span><br><span class="line">		</span><br><span class="line">	&#125;</span><br><span class="line">		</span><br><span class="line"></span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>我们可以看到d这个变量，它不在任何一个作用域里面，所以d是个全局变量。在任何地方可以使用。<br><img src="https://img-blog.csdnimg.cn/ebaff02633c14e7ca6a886d3f5239474.png" alt="在这里插入图片描述"></p>
<p>那么局部变量和全局变量名一样怎么办？<br>那么会优先使用局部变量，就近原则。</p>

            <!--[if lt IE 9]><script>document.createElement('audio');</script><![endif]-->
            <audio id="audio" loop="1" preload="auto" controls="controls" data-autoplay="false">
                <source type="audio/mpeg" src="">
            </audio>
            
                <ul id="audio-list" style="display:none">
                    
                        
                            <li title="0" data-url="http://link.hhtjim.com/163/425570952.mp3"></li>
                        
                    
                        
                            <li title="1" data-url="http://link.hhtjim.com/163/425570952.mp3"></li>
                        
                    
                </ul>
            
        </div>
        
        
    <div id="gitalk-container" class="comment link"
		data-enable="true"
        data-ae="true"
        data-ci=""
        data-cs=""
        data-r=""
        data-o=""
        data-a=""
        data-d="false"
    >查看评论</div>


    </div>
    
        <div class="side">
            <ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#1-%E7%AC%AC%E4%B8%80%E4%B8%AAC%E8%AF%AD%E8%A8%80%E7%A8%8B%E5%BA%8F"><span class="toc-number">1.</span> <span class="toc-text">1. 第一个C语言程序</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#2-%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B"><span class="toc-number">2.</span> <span class="toc-text">2. 数据类型</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#3-%E5%8F%98%E9%87%8F%EF%BC%8C%E5%B8%B8%E9%87%8F"><span class="toc-number">3.</span> <span class="toc-text">3. 变量，常量</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#3-1-%E5%8F%98%E9%87%8F%E7%9A%84%E5%AE%9A%E4%B9%89"><span class="toc-number">3.1.</span> <span class="toc-text">3.1 变量的定义</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-1-2-%E5%8F%98%E9%87%8F%E7%9A%84%E5%91%BD%E5%90%8D%E8%A7%84%E5%88%99"><span class="toc-number">3.2.</span> <span class="toc-text">3.1.2 变量的命名规则</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-2-%E5%8F%98%E9%87%8F%E7%9A%84%E4%B9%8B%E9%97%B4%E7%9A%84%E8%BF%90%E7%AE%97"><span class="toc-number">3.3.</span> <span class="toc-text">3.2 变量的之间的运算</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-3-%E5%B8%B8%E9%87%8F"><span class="toc-number">3.4.</span> <span class="toc-text">3.3 常量</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#3-3-1-const%E5%B8%B8%E9%87%8F"><span class="toc-number">3.4.1.</span> <span class="toc-text">3.3.1 const常量</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-3-2-define%E5%B8%B8%E9%87%8F"><span class="toc-number">3.4.2.</span> <span class="toc-text">3.3.2 define常量</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-4-%E5%B1%80%E9%83%A8%E5%8F%98%E9%87%8F%E3%80%81%E5%85%A8%E5%B1%80%E5%8F%98%E9%87%8F"><span class="toc-number">3.5.</span> <span class="toc-text">3.4 局部变量、全局变量</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#3-4-1-%E4%BD%9C%E7%94%A8%E5%9F%9F"><span class="toc-number">3.5.1.</span> <span class="toc-text">3.4.1 作用域</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-4-2-%E5%8F%98%E9%87%8F%E7%9A%84%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F"><span class="toc-number">3.5.2.</span> <span class="toc-text">3.4.2 变量的生命周期</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-5-%E5%85%A8%E5%B1%80%E5%8F%98%E9%87%8F"><span class="toc-number">3.6.</span> <span class="toc-text">3.5 全局变量</span></a></li></ol></li></ol>
        </div>
    
</div>


    </div>
</div>
</body>

<script src="//cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.js"></script>


<script src="//lib.baomitu.com/jquery/1.8.3/jquery.min.js"></script>
<script src="/my_blog.gitee.io/js/plugin.js"></script>
<script src="/my_blog.gitee.io/js/typed.js"></script>
<script src="/my_blog.gitee.io/js/diaspora.js"></script>


<link rel="stylesheet" href="/my_blog.gitee.io/photoswipe/photoswipe.css">
<link rel="stylesheet" href="/my_blog.gitee.io/photoswipe/default-skin/default-skin.css">


<script src="/my_blog.gitee.io/photoswipe/photoswipe.min.js"></script>
<script src="/my_blog.gitee.io/photoswipe/photoswipe-ui-default.min.js"></script>


<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">
    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>
    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">
        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>
        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">
            <div class="pswp__top-bar">
                <!--  Controls are self-explanatory. Order can be changed. -->
                <div class="pswp__counter"></div>
                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>
                <button class="pswp__button pswp__button--share" title="Share"></button>
                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>
                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>
                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                      <div class="pswp__preloader__cut">
                        <div class="pswp__preloader__donut"></div>
                      </div>
                    </div>
                </div>
            </div>
            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div> 
            </div>
            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>
            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>
            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>
        </div>
    </div>
</div>






</html>
